<?php
/*
// +----------------------------------------------------------------------------------------
// | Icoterie Development Framework Intelligence Enterprise Management Priority Selective
// +----------------------------------------------------------------------------------------
// | Copyright (c) [2019] [Honor Full Epoch Educational Science Technology Hebei Co., Ltd.]
// | website  http://dev.icoterie.top http://www.ihfe.top
// +----------------------------------------------------------------------------------------
// | [Icoterie Development Framework] is licensed under the Mulan PSL v2.
// +----------------------------------------------------------------------------------------
// | You can use this software according to the terms and conditions of the Mulan PSL v2.
// +----------------------------------------------------------------------------------------
// | Licensed ( http://license.coscl.org.cn/MulanPSL2 )
// +----------------------------------------------------------------------------------------
// | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER
// | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR
// | FIT FOR A PARTICULAR PURPOSE.
// +----------------------------------------------------------------------------------------
// | See the Mulan PSL v2 for more details.
// +----------------------------------------------------------------------------------------
// | Author: Reflux Lewse,Red Shadow Sue
// +----------------------------------------------------------------------------------------
// | 服务注册类
// +----------------------------------------------------------------------------------------
*/
namespace core\basic;
/**
 * @method void register()
 * @method void boot()
 */
class Service{
	public function __construct()
    {
		spl_autoload_register([__CLASS__,'autoload']);//自动注册加载函数
		set_error_handler('self::setExceptionHandler');// 设置错误处理函数
		set_exception_handler('self::fatalExceptionHandler');// 设置异常捕获函数 
		register_shutdown_function('self::endExceptionHandler');// 注册异常中止函数
	}
    
	/** 
	* 加载方法
	* @access protected
	* @param string	$c	类库
	*/ 
    private function autoload($c){
		if (strpos($c, DS)) {
			$f = DEV_PATH . $c . '.php';
        }
		if(file_exists($f)){
			require_once DEV_PATH . $c . '.php';
		}
    }
	
	/** 
	* 错误处理 
	* @access public
	* @param int	$n	错误代码
	* @param string	$m	错误信息
	* @param string	$f	错误文件
	* @param int	$l	错误行号
	* @echo string	
	*/ 
	public static function setExceptionHandler($n, $m, $f, $l) 
	{ 
		if (! (error_reporting() & $n)){return;}

        switch ($n) {
            case E_ERROR:
                $e = 'ERROR';
                break;
            case E_WARNING:
                $e = 'WARNING';
                break;
            case E_PARSE:
                $e = 'PARSE';
                break;
            case E_NOTICE:
                $e = 'NOTICE';
                break;
            case E_RECOVERABLE_ERROR:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_USER_ERROR:
                $e = 'FATAL ERROR';
                break;
            default:
                $e = 'UNKNOW';
                break;
        }
        $i = "<h3>".$e
			. ":</h3>\n <p><b>Code:</b>". $n
			. "</p>\n <p><b>Desc:</b>". $m
			. "</p>\n <p><b>File:</b>". $f
			. "</p>\n<p><b>Line:</b>". $l
			. "</p>\n";
        
        if ($e == 'WARNING' || $e == 'NOTICE') {
            echo $i;
        }
	} 
	
	/** 
	* 异常捕获
	* @access public
	* @param string	$$e->getMessage()	错误信息 
	* @param string	$e->getFile()		错误文件 
	* @param int	$e->getLine()		错误行号 
	* @echo string	
	*/ 
    public static function fatalExceptionHandler($e)
    {
		$i = "<p><b>Desc:</b>". $e->getMessage()
			. "</p>\n <p><b>File:</b>". $e->getFile()
			. "</p>\n <p><b>Line:</b>". $e->getLine()
			. "</p>\n";
		echo $i;
    }
	 
	/** 
	* 致命错误
	* @access public
	* @param int	$e['type']		错误代码 
	* @param string	$e['message']	错误信息 
	* @param string	$e['file']		错误文件 
	* @param int	$e['line']		错误行号 
	*/ 
	public static function endExceptionHandler() { 
		$e = error_get_last(); 
		switch ($e['type']) { 
			case 1: 
				self::setExceptionHandler($e['type'], $e['message'], $e['file'], $e['line']); 
				break; 
		} 
	} 
}
?>